package com.jk1.bianligou.blg.controller;

import cn.hutool.core.bean.BeanUtil;
import com.jk1.bianligou.blg.aop.MerchantCheck;
import com.jk1.bianligou.blg.common.BaseResponse;
import com.jk1.bianligou.blg.domain.dto.order.CreateOrderRequest;
import com.jk1.bianligou.blg.domain.vo.OrderVO;
import com.jk1.bianligou.blg.service.OrderService;
import com.jk1.bianligou.blg.utils.ResultUtils;
import com.jk1.bianligou.blg.utils.ThrowUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author WHH
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/create")
    @ApiOperation("生成订单")
    public BaseResponse<Long> createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest httpServletRequest) {
        ThrowUtils.throwIfParamsIsNull(request);
        return ResultUtils.success(orderService.createOrder(request, httpServletRequest));
    }

    @GetMapping("/user")
    @ApiOperation("通过用户id获取订单")
    public BaseResponse<List<OrderVO>> getByUserId(@RequestParam Long id) {
        ThrowUtils.throwIfParamsIsNull(id);
        return ResultUtils.success(orderService.getByUserId(id));
    }

    @PostMapping("/complete/{id}")
    @ApiOperation("将订单标记为已完成")
    public BaseResponse<Boolean> markOrderSuccess(@PathVariable Long id) {
        ThrowUtils.throwIfParamsIsNull(id);
        return ResultUtils.success(orderService.markSuccess(id));
    }

    @GetMapping("/show")
    @MerchantCheck
    @ApiOperation("分类展示所有订单")
    public BaseResponse<Map<Integer, List<OrderVO>>> showOrders() {
        return ResultUtils.success(orderService.showOrders());
    }

    @GetMapping("/{id}")
    @MerchantCheck
    @ApiOperation("根据ID获取订单")
    public BaseResponse<OrderVO> getById(@PathVariable Long id) {
        ThrowUtils.throwIfParamsIsNull(id);
        return ResultUtils.success(BeanUtil.copyProperties(orderService.getById(id), OrderVO.class));
    }
}
